package com.qxzn.app.util;

import android.content.Context;
import android.content.SharedPreferences;

public class SharePreferenceUtil {

    private static SharePreferenceUtil spUtil;
    private static SharedPreferences userSp;
    private static SharedPreferences foreverSp;
    private static SharedPreferences.Editor userEdit;
    private static SharedPreferences.Editor foreverEdit;
    private Context mContext;

    private SharePreferenceUtil(Context context) {
        this.mContext = context.getApplicationContext();
        userSp = mContext.getApplicationContext().getSharedPreferences("user", Context.MODE_PRIVATE);
        foreverSp = mContext.getApplicationContext().getSharedPreferences("love", Context.MODE_PRIVATE);
        userEdit = userSp.edit();
        foreverEdit = foreverSp.edit();
    }

    public static SharePreferenceUtil getInstance(Context context) {
        if (spUtil == null) {
            synchronized (SharePreferenceUtil.class) {
                if (spUtil == null) {
                    spUtil = new SharePreferenceUtil(context);
                }
            }
        }
        return spUtil;
    }

    public static void setUserName(Context context, String value) {
        getInstance(context).setString("username", value);
    }

    public static String getUserName(Context context) {
        return getInstance(context).getString("username");
    }

    public static void setPwd(Context context, String value) {
        getInstance(context).setString("pwd", value);
    }

    public static String getPwd(Context context) {
        return getInstance(context).getString("pwd");
    }

    public static void setDeviceId(Context context, String value) {
        getInstance(context).setString("device_id", value);
    }

    public static String getDeviceId(Context context) {
        return getInstance(context).getString("device_id");
    }

    public static void setDeviceName(Context context, String value) {
        getInstance(context).setString("device_name", value);
    }

    public static String getDeviceName(Context context) {
        return getInstance(context).getString("device_name");
    }

    public static void setNickname(Context context, String value) {
        getInstance(context).setString("nickname", value);
    }

    public static String getNickname(Context context) {
        return getInstance(context).getString("nickname");
    }

    public static void setPortrait(Context context, String value) {
        getInstance(context).setString("portrait", value);
    }

    public static String getPortrait(Context context) {
        return getInstance(context).getString("portrait");
    }

    public static void setSex(Context context, String value) {
        getInstance(context).setString("sex", value);
    }

    public static String getSex(Context context) {
        return getInstance(context).getString("sex");
    }

    public static void setAge(Context context, int value) {
        getInstance(context).setInt("age", value);
    }

    public static int getAge(Context context) {
        return getInstance(context).getInt("age");
    }

    //存储用户权限。1：访问数据的权限；2：语音播报的权限
    public static void setRoles(Context context, int value) {
        getInstance(context).setInt("roles", value);
    }

    public static int getRoles(Context context) {
        return getInstance(context).getInt("roles");
    }

    /**
     * 设置接收聊天消息的用户，用于测试
     *
     * @param context
     * @param value
     */
    public static void setChatReceiver(Context context, String value) {
        getInstance(context).setString("chat_receiver", value);
    }

    public static String getChatReceiver(Context context) {
        return getInstance(context).getString("chat_receiver");
    }

    public static void setChatMsgNum(Context context, int value) {
        getInstance(context).setInt("chat_msg_num", value);
    }

    public static int getChatMsgNum(Context context) {
        return getInstance(context).getInt("chat_msg_num");
    }

    public static void setPhone(Context context, String value) {
        getInstance(context).setString("phone", value);
    }

    public static String getPhone(Context context) {
        return getInstance(context).getString("phone");
    }

    public static void setAddress(Context context, String value) {
        getInstance(context).setString("address", value);
    }

    public static String getAddress(Context context) {
        return getInstance(context).getString("address");
    }

    public static void setTuyaUid(Context context, String value) {
        getInstance(context).setString("tuya_uid", value);
    }

    public static String getTuyaUid(Context context) {
        return getInstance(context).getString("tuya_uid");
    }

    public static void setTuyaToken(Context context, String value) {
        getInstance(context).setString("tuya_token", value);
    }

    public static String getTuyaToken(Context context) {
        return getInstance(context).getString("tuya_token");
    }

    public static void setFirstStart(Context context, boolean value) {
        getInstance(context).setBoolean("first_start", value);
    }

    public static boolean isFirstStart(Context context) {
        return getInstance(context).getBoolean("first_start", true);
    }


    public static void setScreenWidth(Context context, int value) {
        getInstance(context).setInt1("screen_width", value);
    }

    public static int getScreenWidth(Context context) {
        return getInstance(context).getInt1("screen_width", 400);
    }

    public static void setScreenHeight(Context context, int value) {
        getInstance(context).setInt1("screen_height", value);
    }

    public static int getScreenHeight(Context context) {
        return getInstance(context).getInt1("screen_height", 600);
    }

    public static void setTestServer(Context context, boolean value) {
        getInstance(context).setBoolean1("is_test_server", value);
    }

    public static boolean isTestServer(Context context) {
        return getInstance(context).getBoolean1("is_test_server");
    }

    public static void setShowLog(Context context, boolean value) {
        getInstance(context).setBoolean1("is_show_log", value);
    }

    public static boolean isShowLog(Context context) {
        return getInstance(context).getBoolean1("is_show_log");
    }

    public static void setEmergencyPhone(Context context, String value) {
        getInstance(context).setString("emergency_phone", value);
    }

    public static String getEmergencyPhone(Context context) {
        return getInstance(context).getString("emergency_phone");
    }

    public static void setNoticeUpdateDate(Context context, String value) {
        getInstance(context).setString("notice_update_date", value);
    }

    public static String getNoticeUpdateDate(Context context) {
        return getInstance(context).getString("notice_update_date");
    }


    //存储用户字符串数据
    public void setString(String key, String value) {
        userEdit.putString(key, value).commit();
    }

    //获取用户户字符数据---不带默认值
    public String getString(String key) {
        return getString(key, "");
    }

    //获取用户字符串数据---带默认值
    public String getString(String key, String defValue) {
        return userSp.getString(key, defValue);
    }

    //存储用户boolean数据
    public void setBoolean(String key, boolean value) {
        userEdit.putBoolean(key, value).commit();
    }

    //获取用户boolean数据---不带默认值
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    //获取用户boolean数据---带默认值
    public boolean getBoolean(String key, boolean defValue) {
        return userSp.getBoolean(key, defValue);
    }

    //存储用户long数据
    public void setLong(String key, long value) {
        userEdit.putLong(key, value).commit();
    }

    //获取用户long数据---不带默认值
    public long getLong(String key) {
        return getLong(key, 0);
    }

    //获取用户long数据---带默认值
    public long getLong(String key, long defValue) {
        return userSp.getLong(key, defValue);
    }

    //获取用户int数据
    public void setInt(String key, int value) {
        foreverEdit.putInt(key, value).commit();
    }

    public int getInt(String key) {
        return getInt(key, 0);
    }

    public int getInt(String key, int defValue) {
        return userSp.getInt(key, defValue);
    }

    //清除用户数据
    public void clear() {
        userEdit.clear().commit();
    }


    //清除全局数据
    public void clear1() {
        foreverEdit.clear().commit();
    }

    //存储全局数据，用户退出登录不清除----String
    public void setString1(String key, String value) {
        foreverEdit.putString(key, value).commit();
    }

    public String getString1(String key) {
        return getString1(key, "");
    }

    public String getString1(String key, String defValue) {
        return foreverSp.getString(key, defValue);
    }

    //存储全局数据，用户退出登录不清除----int
    public void setInt1(String key, int value) {
        foreverEdit.putInt(key, value).commit();
    }

    public int getInt1(String key) {
        return getInt1(key, 0);
    }

    public int getInt1(String key, int defValue) {
        return foreverSp.getInt(key, defValue);
    }

    //存储全局数据，用户退出登录不清除----boolean
    public void setBoolean1(String key, boolean value) {
        foreverEdit.putBoolean(key, value).commit();
    }

    public boolean getBoolean1(String key) {
        return getBoolean1(key, false);
    }

    public boolean getBoolean1(String key, boolean defValue) {
        return foreverSp.getBoolean(key, defValue);
    }

    /**
     * 存储多个用户数据，暂不提交，后面调用 {@link #userSpMutCommit}
     *
     * @param key
     * @param value
     */
    public void setMutString(String key, String value) {
        userEdit.putString(key, value);
    }

    public void setMutInt(String key, int value) {
        userEdit.putInt(key, value);
    }

    //统一提交数据
    public void userSpMutCommit() {
        userEdit.commit();
    }

    /**
     * 存储多个全局数据，暂不提交，后面调用 {@link #userSpMutCommit1}
     *
     * @param key
     * @param value
     */
    public void setMutString1(String key, String value) {
        foreverEdit.putString(key, value);
    }

    //统一提交全局数据
    public void userSpMutCommit1() {
        foreverEdit.commit();
    }

}
